layui.use(['form','table','element','laytpl','layer'], function() {
  var table = layui.table;
  var form = layui.form;
  
  getUserInfo();
  function  getUserInfo(){
      $.ajax({
          url:"/ssm/consumer/loginInfo.do",
          type:"POST",
          dataType:"json",
          success:function(data){
          	info = data.body
             if(info!=null){
          	   $(".userLogin").hide();
          	   $("#username").text(info.real_name);
          	   $(".userNotLogin").show();
          	 table.render({
          	    elem: '#supplierTable'
          	    ,url:'/ssm/productComment/r.do'
          	    ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
          	    ,where:{
          	    	uid:info.id
          	    }
          	    ,page : true
          	    ,height : "full-125"
          	    ,cols: [[
          	      {field:'id', width:80, title: 'ID',type:'numbers'}
          	      ,{field:'username', title: '消费者'}
          	      ,{field:'content',  title: '留言内容'}
          	      ,{field:'create_date', title: '留言时间'}
          	      ,{field:'reply',  title: '回复'}
          	      ,{align:'center', toolbar: '#barTpl', width: 120, title: '操作'}
          	    ]]
          	  });
          	   
             }else{
          	   location.href="/ssm/consumer/login.html";
             }
          } 
      })
   }
   
  

  //工具条点击事件
  	layui.table.on('tool(table)', function(obj){
  		var data = obj.data;
  		var layEvent = obj.event;

  		if(layEvent === 'edit'){ //修改
  			showEditModel(data);
  		} else if(layEvent === 'del'){ //删除
  			doDelete(data);
  		} else if(layEvent == 'detail'){
  			showPermDialog(data.id);
  		}else if(layEvent == 'operateAuth'){
  			perAuthWin(data.id,data.role_name);
  		}
  	});


  	//添加按钮点击事件
  	$("#addBtn").click(function(){
  		showEditModel(null);
  		return false;
  	});

  

  	//显示表单弹窗
    function showEditModel(data){
        layer.open({
            type: 1,
            title: data=="回复",
            area: '90%',
            offset: 'auto',
            content: $("#addModel").html(),
            success:function(){
            	form.render('select');
            }
        });
        if(data!=null){
            $("#editForm input[id=id]").val(data.id);
            $("#editForm input[id=content]").val(data.content);
            form.render('select');
        }
        $("#btnCancel").click(function(){
            layer.closeAll('page');
        });

    }

    	//表单提交事件
    layui.form.on('submit(btnSubmit)', function(data){
            var qUrl="/ssm/productComment/cud.do";
            $.post(qUrl,{
                id : $("#id").val(),
                type:'u',
                username : $("#username").val(),
                reply : $("#reply").val(),
            },function(res){
             if(res.code=="01"){
                 layer.closeAll();
                 layer.msg("保存成功");
                 //刷新父页面
                 layui.table.reload('supplierTable', {});
             }else{
                 layer.msg("保存失败");
             }
         });
            return false;
    });

    
	//删除
	function doDelete(data){
        layer.confirm('确定删除吗？',{icon:3, title:'提示信息'},function(index){
            $.post("/ssm/productComment/del.do",{
            		id : data.id
            },function(data){
                if(data.code=="01"){
                	layer.close(index);
                	layui.table.reload('supplierTable', {});
                    
                }else{
                	layer.close(index);
                    layer.alert("删除失败");
                    
                }

            })
        });
	}

});